﻿create schema workflow;

create table wf_FieldControl
(
   Id int primary key,
   Name varchar(50) not null,
   Code varchar(50) not null
);

create table wf_Form
(
   Id serial primary key,
   Name varchar(50) not null,
   State int not null default 1,
   Version int not null default 0
);

create table wf_Field
(
   Id serial primary key,
   FormId int references wf_Form(Id) not null,
   ControlId int references wf_FieldControl(Id) not null,
   Name varchar(50) not null,
   Nullable boolean not null default true,
   Editable boolean not null default true,
   Source int not null default 0,
   State int not null default 1,
   Argument varchar(200),
   DefaultValue text
);

create table wf_FormInstance
(
   Id serial primary key,
   TemplateFormId int not null,
   Name varchar(50) not null,
   State int not null default 1,
   Version int not null
);

create table wf_FieldInstance
(
   Id serial primary key,
   TemplateFieldId int not null,
   FormId int references wf_FormInstance(Id) not null,
   ControlId int references wf_FieldControl(Id) not null,
   Name varchar(50) not null,
   Nullable boolean not null default true,
   Editable boolean not null default true,
   Source int not null default 0,
   State int not null default 1,
   Argument varchar(200),
   DefaultValue text
);

create table wf_Flow
(
   Id serial primary key,
   FormId int not null references wf_FormInstance(Id),
   Name varchar(50) not null,
   Description text,
   State int not null default 1
);

create table wf_RoleFlow
(
   RoleId int not null references sys_Role(Id),
   FlowId int not null references wf_Flow(Id),
   primary key(RoleId, FlowId)
);

create table wf_Node
(
   Id serial primary key,
   FlowId int not null references wf_Flow(Id),
   ActionId int not null default 0,  -- 0 未定义 1 指定个人审批 2 指定多人审批 3 角色审批 
									 -- 4 直属上级审批 5 直属上级循环审批 6 职位审批
   Name varchar(50) not null,
   ToUser int references sys_User(Id),
   ToDept int references sys_Org(Id),
   ToPosition int references sys_Position(Id),
   ToRole int references sys_Role(Id),
   InDept boolean,  -- 是否只只在相同部门审批
   AllowSkip boolean, -- 是否允许无法审批时跳过此项
   Argument text,
   State int not null default 1
);

create table wf_Transition
(
   Id serial primary key,
   FlowId int not null,
   NodeId int not null,
   PreNodeId int,
   NextNodeId int,
   Condition text,
   State int not null default 1
);

create table wf_FlowInstance
(
   Id serial primary key,
   FlowId int not null references wf_Flow(Id),
   FormId int null,
   ObjectId int null,
   CreateUserId int references sys_User(Id), -- 创建人
   CreateDeptId int references sys_Org(Id), -- 创建人部门
   Description text,
   State int not null default 1,
   CreationTime timestamp not null default now(),
   UpdatedTime timestamp not null default now()
);

create table wf_NodeInstance
(
   Id serial primary key,
   Name text,
   FlowId int not null references wf_Flow(Id),
   NodeId int not null references wf_Node(Id),
   FlowInstanceId int not null references wf_FlowInstance(Id),
   FormId int null,
   FormInstanceId int null,
   CreateUserId int references sys_User(Id), -- 当前节点创建人
   WorkUserId int references sys_User(Id), -- 当前节点处理人
   State int not null default 1, -- 状态 0 关闭 1 进行中 2 暂停 3 结束-通过 4 结束-未通过
   Result text, -- 处理结果
   CreationTime timestamp not null default now(),
   UpdatedTime timestamp not null default now()
);

create table wf_Dictionary
(
   Id serial primary key,
   Value varchar(100) not null,
   SubGroup varchar(200) not null,
   Sort int not null default 0,
   State int not null default 1
);


create table wf_Workflow
(
   Id serial primary key,
   FormInstanceId int not null,
   Name text not null,
   Description text,
   State int not null default(1)
);


create table wf_Task
(
   Id serial primary key,
   WorkType int not null,
   ActionType int not null,
   ToUser int,
   ToDept int,
   ToPosition int,
   ToRole int,
   ToUserList int[],
   InDept boolean not null default true,
   AllowSkip boolean not null default true,
   Argument text,
   State int not null default 1,
   Remark text
);

--create table wf_path
--(
--   id serial primary key,
--   flowid int not null,
--   typeid int not null, -- 1 执行task  2 条件判断
--   name text,
--   condition text,
--   taskid int,
--   true_next int,
--   false_next int,
--   remark text
--);

create table wf_FlowObject
(
    Id serial primary key,
    FlowId int not null,	
    FormInstanceId int not null,
    DataId int not null,
    CreateUserId int not null,
    CreateDeptId int not null,
    Description text,
    State int not null default 1,
    CreationTime timestamp not null default now(),
    UpdatedTime timestamp,
	Argument text
);

create table wf_TaskObject
(
   Id serial primary key,   
   TaskId int not null,
   StepId int not null,
   FlowObjectId int not null,
   WorkUserId int not null,
   TaskName text not null,
   Result text null,
   State int not null default 1,
   CreationTime timestamp not null default now(),
   UpdatedTime timestamp null
);

create table wf_FlowObject_CC
(
   Id serial primary key,
   FlowObjectId int not null,
   ToUser int not null,
   CreationTime timestamp not null default now()
);

--create table wf_options
--(
--   Id serial primary key,
--   TypeId int,
--   UserId int,
--   FlowId int,
--   OptionValue text
--);

--create table wf_FlowInstance_FormInstance
--(
--   FlowInstanceId int not null,
--   FormInstanceId int not null,
--   TaskId int not null,
--   primary key(FlowInstanceId, FormInstanceId, TaskId)
--);

create table wf_flowrole
(
    flowid int not null,
    roleid int not null,
    primary key(flowid, roleid)
);


-- 数据
insert into wf_FieldControl(Id, Name, Code) values(1, '文本框', 'textbox'), (2, '多行文本框', 'textarea'), (3, '日期控件', 'date'), (4, '下拉框', 'select'), (5, '整数输入框', 'digits'), (6, '浮点数输入框', 'number'), (7, '隐藏控件', 'hidden');



-- 扩展控件类型
-- 8. 标签控件
insert into wf_FieldControl(Id, Name, Code) values(8, '标签控件', 'label');
-- 9. 日期时间控件
insert into wf_FieldControl(Id, Name, Code) values(9, '日期时间控件', 'datetime');

-- 10. 表格控件
insert into wf_FieldControl(Id, Name, Code) values(10, '表格控件', 'table');

-- 11. 文件上传控件
insert into wf_FieldControl(Id, Name, Code) values(11, '文件上传控件', 'upload');

create table wf_Table
(
   Id serial primary key,
   State int not null default 1
);

create table wf_Column
(
   Id serial primary key,
   TableId int not null references wf_Table(Id),
   ColumnName text not null,
   ControlId int not null references wf_FieldControl(Id),
   Nullable boolean not null default true,
   Source int not null default 0,
   State int not null default 1,
   Argument text,
   DefaultValue text
);